package com.shopnight.controller.circle;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopnight.entity.Post;
import com.shopnight.entity.PostComment;
import com.shopnight.entity.User;
import com.shopnight.mapper.UserMapper;
import com.shopnight.result.ApiResult;
import com.shopnight.service.PostCategoryService;
import com.shopnight.service.PostCommentService;
import com.shopnight.service.PostService;
import com.shopnight.service.UserService;
import com.shopnight.vo.PostListVo;
import com.shopnight.vo.WallUserInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags = {"postConteoller"},description = "表白墙业务所需接口")
public class PostConteoller {

    @Autowired
    PostService postService;

    @Autowired
    PostCommentService postCommentService;

    @Autowired
    PostCategoryService postCategoryService;

    @Autowired
    UserService userService;


    @PostMapping("/token/addPost")
    @ApiOperation(value = "新增帖子",notes = "")
    public ApiResult addPost(@RequestBody Post post){

        //插入数据库
        return ApiResult.ok( postService.addPost(post) );
    }



    @PostMapping("/getPostList")
    @ApiOperation(value = "获取帖子列表",notes = "传page，pageSize，orgId，categoryId")
    public ApiResult getPostList(@RequestBody Map<String,Integer> map){
        Page page =  new Page<>(  map.get("page"), map.get("pageSize") );
        Page<PostListVo> postList = postService.getPostList(page, map.get("orgId") , map.get("categoryId"));

        //返回数据
        return ApiResult.ok(postList);
    }

    @PostMapping("/getPostList/recommend")
    @ApiOperation(value = "获取帖子推荐列表",notes = "传page，pageSize，orgId，categoryId")
    public ApiResult getPostListRecommend(@RequestBody Map<String,Integer> map){
        Page page =  new Page<>(  map.get("page"), map.get("pageSize") );
        Page<PostListVo> postList = postService.getPostListRecommend(page, map.get("orgId") , map.get("categoryId"));

        //返回数据
        return ApiResult.ok(postList);
    }

    @PostMapping("/getPostCategory")
    @ApiOperation(value = "获取帖子的分类",notes = "")
    public ApiResult getPostCategory(){
        //获取数据
        return ApiResult.ok(postCategoryService.list());
    }


    @PostMapping("/token/addPostComment")
    @ApiOperation(value = "发布帖子评论",notes = "")
    public ApiResult addPostComment(@RequestBody PostComment postComment){
        //返回数据
        return ApiResult.ok( postCommentService.addPostComment(postComment) );
    }


    @GetMapping("/getPostInfo")
    @ApiOperation(value = "查询帖子详情",notes = "传id")
    public ApiResult getPostInfo(@RequestParam Integer id){
        //返回数据
        return ApiResult.ok( postService.getPostInfo(id));
    }

    @GetMapping("/token/delPost")
    @ApiOperation(value = "删除帖子",notes = "传id")
    public ApiResult delPost(@RequestParam Integer id){
        //返回数据
        return postService.delPost(id);
    }

    @PostMapping("/getPostComment")
    @ApiOperation(value = "查询帖子评论",notes = "传page，pageSize，id")
    public ApiResult getPostComment(@RequestBody Map<String,Integer> map){
        Page page =  new Page<>(  map.get("page"), map.get("pageSize") );
        //返回数据
        return ApiResult.ok( postCommentService.getPostComment(page,map.get("id")) );
    }


    @PostMapping("/token/getMyPost")
    @ApiOperation(value = "查询我的帖子",notes = "传page，pageSize")
    public ApiResult getMyPost(@RequestBody Map<String,Integer> map){
        Page page =  new Page<>(  map.get("page"), map.get("pageSize") );
        //返回数据
        return ApiResult.ok( postService.getMyPost(page) );
    }

    @PostMapping("/getTheyPost")
    @ApiOperation(value = "查询他人帖子",notes = "传 userId,page，pageSize")
    public ApiResult getTheyPost(@RequestBody Map<String,Integer> map){
        Page page =  new Page<>(  map.get("page"), map.get("pageSize") );
        //返回数据
        return ApiResult.ok( postService.getTheyPost(Integer.valueOf( map.get("userId").toString() ) ,page) );
    }


    @GetMapping("/token/getMessageList")
    @ApiOperation(value = "查询我的帖子消息列表",notes = "无需传参")
    public ApiResult getMessageList(){
        //返回数据
        return ApiResult.ok( postService.getMessageList() );
    }

    @GetMapping("/getWallUserInfoById")
    @ApiOperation(value = "获取用户信息",notes = "userId,需传用户id")
    public ApiResult getWallUserInfoById(Integer userId)  {

        User byId = userService.getById(userId);
        WallUserInfoVo wallUserInfoVo = new WallUserInfoVo();
        BeanUtils.copyProperties(byId,wallUserInfoVo);
        return ApiResult.ok(wallUserInfoVo);

    }



}
